package snsdbook.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import snsdbook.engine.PaginatedContentList;
import util.SQLUtil;
import util.Util;

/**
 * Servlet implementation class ContentListServlet
 */
public class ContentListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContentListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  int page = 0;
    if (request.getParameterMap().containsKey("page")) {
      page = Integer.valueOf(request.getParameter("page"));
    }
    
    request.setAttribute("header_title", "YU LUKING ARE ET MAI FRIJJ");
    
    String search = "";
    String where = "";
    String[] tags = new String[0];
    if (request.getParameterMap().containsKey("tag")) {
      tags = request.getParameterValues("tag");
    }
    
    if (tags.length > 0) {
      for (String tag : tags) {
        if (!search.equals("")) {
          search += " ";
        }
        search += tag;
      }
      
      where = "tag";
    } else if (request.getParameterMap().containsKey("search")) {
      search = request.getParameter("search");
      where = request.getParameter("search_where");
    }
    
    String cond = "";
    
    if (!search.equals("")) {
      String[] parsed = search.split(" ");
      ArrayList<String> commands = new ArrayList<String>();
      for (String keyword : parsed) {
        if (where.equals("all")) {
          commands.add("judul LIKE '%" + keyword + "%'");
          commands.add("username LIKE '%" + keyword + "%'");
          commands.add("isi_berita LIKE '%" + keyword + "%'");
          commands.add("nama_tag LIKE '%" + keyword + "%'");
        } else if (where.equals("user")) {
          commands.add("username LIKE '%" + keyword + "%'");
        } else if (where.equals("content")) {
          commands.add("isi_berita LIKE '%" + keyword + "%'");
        } else if (where.equals("title")) {
          commands.add("judul LIKE '%" + keyword + "%'");
        } else if (where.equals("tag")) {
          commands.add("nama_tag LIKE '%" + keyword + "%'");
        }
      }
      
      for (String str : commands) {
        if (!cond.equals("")) {
          cond += " OR ";
        }
        cond += str;
      }
    }
    
    // System.out.println("OK: " + cond);
    String query = "";
    if (cond.equals("")) {
      query = "SELECT id_berita FROM berita";
    } else {
      query = "SELECT id_berita FROM berita, tag WHERE (" +
              cond + ") AND berita.id_tag = tag.id_tag";
    }
    
	  // Get all ids of news
	  String[] ids = SQLUtil.Query(query);
	  
	  if (ids.length != 0) {
  	  // Perform sorting
  	  String base = "";
  	  String wheres = "";
  	  String order = "";
  	  String group = "GROUP BY id_berita";
  	  for (String id : ids) {
  	    if (wheres != "") {
  	      wheres += " OR ";
  	    }
  	    wheres += "id_berita=" + id;
  	  }
  	  
  	  String sort = "time";
      if (request.getParameterMap().containsKey("sort")) {
        sort = request.getParameter("sort");
      }
      if (sort.equals("time")) {
        order = " ORDER BY waktu DESC";
        base = "SELECT id_berita FROM berita";
      } else if (sort.equals("like")) {
        order = " ORDER BY like_count DESC";
        base = "SELECT id_berita, COUNT(id_likes) AS like_count FROM " +
               "(SELECT berita.id_berita, id_likes FROM berita, likes " +
               "WHERE berita.id_berita = likes.id_berita) AS T";
      } else if (sort.equals("comment")) {
        order = " ORDER BY comment_count DESC";
        base = "SELECT id_berita, COUNT(id_komentar) AS comment_count FROM " +
               "(SELECT berita.id_berita, id_komentar FROM berita, komentar " +
               "WHERE berita.id_berita = komentar.id_berita) AS T";
      }
      
      ids = SQLUtil.Query(base + " " + group + " HAVING " + wheres + order);
	  }
	  
	  PaginatedContentList.Display(
        Util.toArrayInt(ids),
        /* show_searchbar =  */ true,
        /* show_comments = */ false,
        /* show_pagination = */ true,
        /* is_short = */ true,
        page,
        request,
        response);
	}

}
